<?php

namespace app\controller;

use app\BaseController;
use think\facade\Db;
use think\facade\Session;
use think\facade\Cookie;


class Admin extends BaseController{
    protected $middleware = [
        'app\middleware\Check' => ['except' => ['login']],
    ];
    public function index(){
        $user = Session::get('admin');
        return view("index",['user'=>$user]);

    }

    public function login(){
        $user = Session::get('admin');
        if(!empty($user)){
            return redirect('index');
        }
        if($this->request->isPost()){
            $captcha = $_POST['yzm'];
            if(!captcha_check($captcha)){
                return "验证码填写有误";die();
            };
        $admin_name = $_POST['admin_name'];
        $res = Db::table('admin')->where('admin_name',$admin_name)->find();
        if(empty($res)){
            return "用户名不存在";die();
        }
        $admin_pwd = $_POST['admin_pwd'];
       if(md5($admin_pwd)!=$res['admin_pwd']){
           return "密码填写错误";die();
       }
       Db::table('admin')->where('admin_name', $admin_name)->update(['lasttime' => date('Y-m-d H:i:s')]);
       Session::set('admin', $res);
       $tdf = $this->request->param('tdf');
       if($tdf){
            //Cookie::set('admin',base64_encode(json_encode($res)),60*60*24*3);
       }
       return redirect('index');
        }
        return view();
    }

    public function logout(){
        Session::delete('admin');
        return redirect('login');
    }
}